$(function(){
    ITENY =
    {
        /**
         * 右侧错误提示
         * @param info 错误信息
         * @param id 标签的ID
         */
        error:function (info,id,time)
        {
            if(time == null){
                time = 3000;
            }
            layer.tips(info,id, {
                tips: [2, '#ff6347'],
                time: time
            });
        },
        success:function (info,id,time)
        {
            if(time == null){
                time = 3000;
            }
            layer.tips(info,id, {
                tips: [2, '#215E21'],
                time: time
            });
        },
        //整数IP转换成IP
        long2ip:function(proper_address) {
            proper_address = proper_address>>>0;
            var output = false;
            if ( !isNaN ( proper_address ) && ( proper_address >= 0 || proper_address <= 4294967295 ) ) { output = Math.floor (proper_address / Math.pow ( 256, 3 ) ) + '.' + Math.floor ( ( proper_address % Math.pow ( 256, 3 ) ) / Math.pow ( 256, 2 ) ) + '.' + Math.floor ( ( ( proper_address % Math.pow ( 256, 3 ) ) % Math.pow ( 256, 2 )) / Math.pow ( 256, 1 ) ) + '.' + Math.floor ( ( ( ( proper_address % Math.pow ( 256, 3 ) ) % Math.pow ( 256, 2 ) ) % Math.pow ( 256, 1 ) ) / Math.pow ( 256, 0 ) );
            }
            return output;
        },
        //重新刷新页面，使用location.reload()有可能导致重新提交
        reloadPage:function(win) {
            var location = win.location;
            location.href = location.pathname + location.search;
        },
        iframe_height:function(){
            def_iframe_height = $("body").height() - $("#B_history").height() - $(".head th").height();
            $("#B_frame").height(def_iframe_height);
        },
        //显示顶部导航时作位置判断，点击左边菜单、上一tab、下一tab时公用
        showTab:function(li) {
            if (li.length) {
                var ul = $('#B_history'),
                    li_offset = li.offset(),
                    li_width = li.outerWidth(true),
                    next_left = $('#J_next').offset().left - 9, //右边按钮的界限位置
                    prev_right = $('#J_prev').offset().left + $('#J_prev').outerWidth(true); //左边按钮的界限位置
                if (li_offset.left + li_width > next_left) { //如果将要移动的元素在不可见的右边，则需要移动
                    var distance = li_offset.left + li_width - next_left; //计算当前父元素的右边距离，算出右移多少像素
                    ul.animate({
                        left: '-=' + distance
                    }, 200, 'swing');
                } else if (li_offset.left < prev_right) { //如果将要移动的元素在不可见的左边，则需要移动
                    var distance = prev_right - li_offset.left; //计算当前父元素的左边距离，算出左移多少像素
                    ul.animate({
                        left: '+=' + distance
                    }, 200, 'swing');
                }
                li.trigger('click');
            }
        },
        //判断显示或创建iframe
        iframeJudge:function(options) {
            var elem = options.elem,
                href = options.href,
                id = options.id,
                topid = options.topid,
                li = $('#B_history li[data-id=' + id + ']');


            if (li.length > 0) {
                //如果是已经存在的iframe，则显示并让选项卡高亮,并不显示loading
                var iframe = $('#iframe_' + id);
                setTimeout(function(){
                    $('#loading').hide();
                },500);
                li.addClass('current');
                if (iframe[0].contentWindow && iframe[0].contentWindow.location.href !== href) {
                    iframe[0].contentWindow.location.href = href;
                }
                $('#B_frame iframe').hide();
                $('#iframe_' + id).show();
                ITENY.showTab(li); //计算此tab的位置，如果不在屏幕内，则移动导航位置
            } else {
                $('#B_frame iframe').hide();
                //创建一个并加以标识
                var iframeAttr = {
                    src: href,
                    id: 'iframe_' + id,
                    frameborder: '0',
                    scrolling: 'auto',
                    height: '100%',
                    width: '100%'
                };
                var iframe = $('<iframe/>').prop(iframeAttr).appendTo('#B_frame');

                $(iframe[0].contentWindow.document).ready(function () {

                    setTimeout(function(){
                        $('#loading').hide();
                    },500);
                    var li = $('<li tabindex="0"><span><a>' + elem.html() + '</a><a class="del" title="关闭此页">关闭</a></span></li>').attr('data-id', id).attr('data-top-id',topid).addClass('current');
                    li.siblings().removeClass('current');
                    li.appendTo('#B_history');
                    ITENY.showTab(li); //计算此tab的位置，如果不在屏幕内，则移动导航位置
                    //$(this).show().unbind('load');
                });
            }
        },
        //显示顶部导航时作位置判断，点击左边菜单、上一tab、下一tab时公用
        showTab:function(li) {
            if (li.length) {
                var ul = $('#B_history'),
                    li_offset = li.offset(),
                    li_width = li.outerWidth(true),
                    next_left = $('#J_next').offset().left - 9, //右边按钮的界限位置
                    prev_right = $('#J_prev').offset().left + $('#J_prev').outerWidth(true); //左边按钮的界限位置
                if (li_offset.left + li_width > next_left) { //如果将要移动的元素在不可见的右边，则需要移动
                    var distance = li_offset.left + li_width - next_left; //计算当前父元素的右边距离，算出右移多少像素
                    ul.animate({
                        left: '-=' + distance
                    }, 200, 'swing');
                } else if (li_offset.left < prev_right) { //如果将要移动的元素在不可见的左边，则需要移动
                    var distance = prev_right - li_offset.left; //计算当前父元素的左边距离，算出左移多少像素
                    ul.animate({
                        left: '+=' + distance
                    }, 200, 'swing');
                }
                li.trigger('click');
            }
        },
        getTopMenuByID:function(mid){
            var ret = {};
            var menu = ITENY.getMenuByID(mid);
            if(menu){
                if(menu.parent){
                    var tmp = ITENY.getTopMenuByID(menu.parent);
                    if(tmp && tmp.id){
                        ret = tmp;
                    }
                }else{
                    ret = menu;
                }
            }
            return ret;
        },
        //通过菜单id查找菜单配置对象
        getMenuByID:function(mid,menugroup){
            var ret = {};
            mid = parseInt(mid);
            // if(!menugroup) menugroup = SUBMENU_CONFIG;
            if(isNaN(mid)){
                ret = menugroup['changyong'];
            }else{
                $.each(menugroup, function (i, o) {
                    if( o.id &&  parseInt(o.id) == mid ){
                        ret = o;
                        return false
                    }else if(o.items){
                        var tmp = ITENY.getMenuByID(mid,o.items);
                        if( tmp.id && parseInt(tmp.id) == mid ){
                            ret = tmp;
                            return false
                        }
                    }
                });
            }
            return ret;
        },
        alert: function(title,msg,icon,timeout,showType){//右下角提示框
            layer.alert(msg, {
                icon: icon,
                title: title,
                time: timeout,
                // offset: 'rb',
                offset: '0px',
                shift: 2,
                shade: 0,
                btn: ['我明白了']
            });
        },
        confirm : function(dofunction,info){
            info = info || '确定是否要进行此操作吗？';
            $.messager.confirm('确认对话框', info, function(r){
                if (r){
                    eval(dofunction);
                }
            });
        },
        doForm : function(dom){//添加||编辑数据库信息
            var isValid = $(dom).form('validate');
            if (!isValid){
                return isValid; // 返回false，终止表单提交
            }
            type = $(dom).attr("method");
            url = $(dom).attr("action");
            data = $(dom).serialize();
            $.ajax({
                type: type,
                url: url,
                data: data,
                dataType:"json",
                beforeSend: function(){
                    $.messager.progress({text:'处理中，请稍候...'});
                },
                success: function(data){
                    $.messager.progress("close");
                    if(!data.status){
                        $.xf.alert('提示信息', data.info,'error');
                    } else {
                        $.xf.alert('提示信息', data.info,'info');
                        $($.xf.dialog_1).dialog('close');
                        $.xf.refresh();
                    }
                },
                error: function(data){
                    $.messager.progress("close");
                    $.xf.alert('提示信息', data.responseText,'error');
                }
            });
        },
        delData : function(id,url,type){//删除数据库信息
            type = type || 'post';
            url = url;
            data = 'id='+id;
            $.ajax({
                type: type,
                url: url,
                data: data,
                dataType:"json",
                beforeSend: function(){
                    $.messager.progress({text:'处理中，请稍候...'});
                },
                success: function(data){
                    $.messager.progress("close");
                    if(!data.status){
                        $.xf.alert('提示信息', data.info,'error');
                    } else {
                        $.xf.alert('提示信息', data.info,'info');
                        $.xf.refresh();
                    }
                },
                error: function(data){
                    $.messager.progress("close");
                    $.xf.alert('提示信息', data.responseText,'error');
                }
            });
        },
        sess_verify : function(url,jumpurl){//登录过期校验
            $.post(url,'',function(data) {
                if(!data.status){
                    $.messager.progress({text:'登录超时，正在跳转登录页面...'});
                    setTimeout("location.href = '"+jumpurl+"';",1000);
                }
            },'json');
        },

        loading : function(msg){//正在努力加载中
            msg = msg || "正在努力加载中...";
            return "<div class='oa-loading'>"+msg+"</span>";
        },
        //移除左侧栏目 发现需要执行两次才能彻底清除
        removeLeftMenu : function(stop,titles){
            //加个判断，防止多次点击重复加载
            var options = $('.easyui-layout').layout('panel', 'west').panel('options');
            if(titles == options.title) return false;
            var leftmenu = $("#leftmenu").accordion("panels");
            $.each(leftmenu, function(i,n) {
                if(n){
                    var t = n.panel("options").title;
                    if(titles && titles.length){
                        for(var k = 0; k < titles.length; k++){
                            if(titles[k] == t) $("#leftmenu").accordion("remove", t);
                        }
                    }else{
                        $("#leftmenu").accordion("remove", t);
                    }
                }
            });
            var pp = $('#leftmenu').accordion('getSelected');
            if(pp) {
                var t = pp.panel('options').title;
                if(titles && titles.length){
                    for(var k = 0; k < titles.length; k++){
                        if(titles[k] == t) $("#leftmenu").accordion("remove", t);
                    }
                }else{
                    $("#leftmenu").accordion("remove", t);
                }
            }
            if(!stop){
                this.removeLeftMenu(true, titles);
            }

        },
        openUrl : function(node){//在新tabs窗口打开页面
            if(node.type){
                if($("#center_tabs").tabs('exists', node.text)){
                    $('#center_tabs').tabs('select', node.text);
                }else{
                    $('#center_tabs').tabs('add',{
                        title: node.text,
                        href: node.url,
                        iconCls: node.icon,
                        closable: true,
                        cache: true,
                        tools:[{
                            iconCls:'icon-mini-refresh',
                            handler:function(){
                                var tab = $("#center_tabs").tabs("getSelected");
                                tab.panel("refresh",tab[0]['baseUrl']);
                            }
                        }]
                    });
                }
            }
        },
        // 'refresh' : function(){//刷新当前已选择的Tabs
        //  var tab = $("#center_tabs").tabs("getSelected");
        //  tab.panel("refresh",tab[0]['baseUrl']);
        // },
        refresh : function(){//刷新当前已选择的Tabs
            selected_panel = $(".tabs").find("li.tabs-selected").index();
            if($(".tabs-panels").children(".panel").eq(selected_panel).find(".pagination-load").size() > 0){
                $(".tabs-panels").children(".panel").eq(selected_panel).find(".pagination-load").click();
            } else {
                var tab = $("#center_tabs").tabs("getSelected");
                tab.panel("refresh",tab[0]['baseUrl']);
            }
        },
        //时间戳转换成日期
        time_format:function (time)
        {
            var timestampNow = Math.round(new Date().getTime()/1000);
            var dif = timestampNow-time;
            var outPut ="";
            // if (dif<=15) outPut ="刚才";
            // else if(dif<60) outPut=dif+"秒前";
            // else if(dif<3600) outPut=Math.round(dif/60)+"分钟前";
            // else if(dif<86400) outPut=Math.round(dif/3600)+"小时前";
            // else if(dif<2592000) outPut=Math.round(dif/86400)+"天前";
            // else {
                var date = new Date(time*1000);
                var hours = date.getHours();
                var minutes = date.getMinutes();
                var seconds = date.getSeconds();
                if(hours<10) hours = "0"+hours;
                if(minutes<10) minutes="0"+minutes;
                outPut = date.getFullYear()+ "-" + Number(date.getMonth() + 1)+"-"+date.getDate()+" "+hours+":"+minutes+":"+seconds;
            // }
            return outPut;
        }
    };
});